package edu.upb.acs.jobsmaps.activity.search;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.activity.MainScreenActivity;
import edu.upb.acs.jobsmaps.activity.voiceRecognition.VoiceRecognitionListenerSearchCompany;
import edu.upb.acs.jobsmaps.voice.VoiceUtils;
/**
 * Created by dani on 5/25/2014.
 */
public class SearchCompanyActivity extends Activity implements OnInitListener {
	private static final String TAG = SearchCompanyActivity.class.getName();

	private SpeechRecognizer sr;
	private Intent intent;
	private TextToSpeech tts;
	private AudioManager amAudioManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_company);

		Log.d(TAG, "initialize activity");


		DisplayMetrics metrics = getResources().getDisplayMetrics();
		int screenWidth = (int) (metrics.widthPixels * 0.80);

		getWindow().setLayout(screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT);

		Button search = (Button) findViewById(R.id.searchButtonCompany);
		search.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				final Spinner spinner = (Spinner) findViewById(R.id.searchCriteriaCompany);
				final String searchCriteria = (String) spinner.getSelectedItem();

				final EditText searchWordField = (EditText) findViewById(R.id.searchWordCompany);
				final String searchWord = searchWordField.getText().toString();

				final String searchCompanyDistance = ((Spinner) findViewById(R.id.searchCompanyDistance)).getSelectedItem().toString();

				System.out.println("Search searchWord = " + searchWord);
				System.out.println("Search searchCompanyDistance = " + searchCompanyDistance);

				Intent i = new Intent(SearchCompanyActivity.this,
						MainScreenActivity.class);
				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				i.putExtra("search_word_company", searchWord);
				i.putExtra("search_criteria_company", searchCriteria);
				i.putExtra("search_distance_company", searchCompanyDistance);

				startActivity(i);
			}
		});

		//the audio manager for microphone
		amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

		//create a new speaker
		tts = new TextToSpeech(this, this);

		Log.d(TAG, "Entering startListening");
		startListening();
	}

	private void startListening() {
		sr = SpeechRecognizer.createSpeechRecognizer(this);

		intent = VoiceUtils.initializeIndent(intent, getApplicationContext());

		sr.setRecognitionListener(new VoiceRecognitionListenerSearchCompany(sr, intent, tts, amAudioManager, this));
		sr.startListening(intent);
	}

	@Override
	protected void onStart() {
		super.onStart();


	}

	@Override
	public void onDestroy(){
		if (tts != null){
			tts.stop();
			tts.shutdown();
		}

		sr.stopListening();
		sr.destroy();
		super.onDestroy();
	}


	//folosit la TextToSpeech
	@Override
	public void onInit(int status) {
		Log.d(TAG, "!!!!!!!!!!!!!! Init voice ");
		if (status == TextToSpeech.SUCCESS) {
			//Speak
			tts.speak("Search criteria", TextToSpeech.QUEUE_FLUSH, null);
			while (tts.isSpeaking()) {
//				Log.d(TAG, "She is speaking");
			}
		} else {
			//Handle initialization error here
			Log.d(TAG, "Initialization for TTS failed!!!");
		}
	}
}
